<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="/js/include.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .layui-upload-img {
            width: 30%;
            height: 30%;
            margin: 0 10px 10px 0;
        }
    </style>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>资源详情明细</legend>
</fieldset>

<form class="layui-form" id="from1" action="">
        <input type="hidden" name="fk_resource_id">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">中文名</label>
            <div class="layui-input-block">
                <input type="text" name="rd_cn_name" lay-verify="username"  placeholder="请输入中文名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">英文名</label>
            <div class="layui-input-block">
                <input type="text" name="rd_name" lay-verify="required"  placeholder="请输入英文名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input type="text" name="rd_description" lay-verify="required" autocomplete="off" placeholder="请输入描述" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字段类型</label>
            <div class="layui-input-block">
                <select   id="type" name="field_type"  lay-filter="fieldtest"   >

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">长度</label>
            <div class="layui-input-block">
                <input type="text" name="rd_length" value="0" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                <button class="layui-btn cancle">取消</button>
            </div>
        </div>

</form>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>

    //获取当前数据库的所有字段类型
    $.ajax({
        url: "/cms/user/get_field_type",
        type:'POST',
        async: false,
        success: function (data) {
            //console.log(data);
            data.data.map(function (item) {
                console.log(item);
                $("#type").append("<option value="+item.name+">"+item.value+"</option>");
            })
            console.log($("#type option:selected").text())
            if($("#type option:selected").text()=="VARCHAR"){
                $('input[name=rd_length]').val(255)
            }

        }
    });
    //下拉框值被更换


    var table_name = "resource_detail";

    layui.use(['form', 'layedit'], function () {
        var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit,
                upload = layui.upload;
        init();
        form.render();
        form.on('submit(demo1)', function (data) {
            console.log(data.field)
            $.ajax({
                url: "/dbm/resource/detail/save",
                //data:data.field,
                type: "post",
                data: {submit_data: JSON.stringify(data.field)},
                success: function (result) {
                    if (result.success) {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.query();
                    } else {
                        layer.msg(result.message)
                    }
                }
            })
            return false;
        });
        //监听下拉框
        form.on('select(fieldtest)', function(data){
            console.log(data.value);
            if(data.value=="VARCHAR"){
                $('input[name=rd_length]').val(255)
                $('input[name=rd_length]').removeAttr("disabled")
            }else if(data.value=="INTEGER"){
                $('input[name=rd_length]').val(11)
                $('input[name=rd_length]').removeAttr("disabled")
            }else if(data.value=="DECIMAL"){
                $('input[name=rd_length]').val(18)
            }else {
                $('input[name=rd_length]').val(0)
                $('input[name=rd_length]').attr("disabled","disabled")
            }


        });

    });

    $(".cancle").click(function () {
        window.parent.layer.closeAll();
    })

    function parseJson(param) {
        var obj = {};
        var keyvalue = [];
        var key = "", value = "";
        var paraString = param.split("&");
        for (var i in paraString) {
            keyvalue = paraString[i].split("=");
            key = keyvalue[0];
            value = keyvalue[1];
            obj[key] = value;
        }
        return obj;
    }

    function init() {
        var url = window.location.search;
        if (url != null && url != "") {
            console.log(url);
            url = url.substr(1, url.length);
            if(parseJson(url).fk_resource_id!=""){
                $("input[name='fk_resource_id']").val(parseJson(url).fk_resource_id);
            }
            if (parseJson(url).id != "") {
                //console.log(parseJson(url).id);

                $.ajax({
                    url: "/layui/table/detail",
                    data: {id: parseJson(url).id, resource_name: table_name},
                    async: false,
                    success: function (data) {
                        if (data.success) {
                            $.each(data.data, function (name, value) {
                                console.log(name)
                                var t = $("#from1" + ' *[name=' + name + ']');
                                if (t.prop("localName") == "textarea") {
                                    $("textarea[name='" + name + "']").val(value)
                                }
                                if (t.prop("localName") == "input") {
                                    $("input[name='" + name + "']").val(value)
                                }
                                if (name == "logo_image") {
                                    $("#logo_image").prop("src", value);
                                }
                                if (t.prop("localName") == "select") {
                                    $("select[name='" + name + "']").val(value)

                                }
                            })
                        } else {
                            layer.msg(data.message)
                        }
                    }
                })
            }
        }
    }

    function upperCase(){
        console.log(789)
        //alert($(this).val());//可以在这里做判断选中值
    };//change：每次选择选项获取选中值
</script>

</body>
</html>